package com.ysm.smfu.view.popu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.view.MutilRadioGroup;

import java.util.HashMap;
import java.util.Map;

public class PopFourListView extends RelativeLayout implements View.OnClickListener {
    private OnSelectListener mOnSelectListener;
    private ExpandPopTabView mExpandPopTabView;
    private CheckBox only_FreeYuYue;
    private MutilRadioGroup lunchTime_group;
    private TextView reset;
    private TextView confirm;
    private CheckBox canuse_holiday;
    private MutilRadioGroup servce_group;
    private MutilRadioGroup num_group;
    private ExpandPopTabView expandPopTabView;

    /**
     * 设置ExpandPopTabView
     *
     * @param expandPopTabView
     */
    public void setExpandPopTabView(ExpandPopTabView expandPopTabView) {
        this.expandPopTabView = expandPopTabView;
    }

    public interface OnSelectListener {
        void getValue(boolean isFreeuse, boolean isUseHoliday, int lunTime, int lunServe, int lunCount);
    }

    Map<Integer, Integer> lunchTimeMap = new HashMap<>();
    Map<Integer, Integer> lunchServeMap = new HashMap<>();
    Map<Integer, Integer> lunchCountMap = new HashMap<>();

    public void setOnSelectListener(ExpandPopTabView expandPopTabView, OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
        mExpandPopTabView = expandPopTabView;
    }

    public PopFourListView(Context context) {
        super(context);
        init(context);
    }

    public PopFourListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public PopFourListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void init(Context context) {
        getData();
        LayoutInflater.from(context).inflate(R.layout.shuaixuan, this, true);
        only_FreeYuYue = (CheckBox) findViewById(R.id.only_FreeYuYue);//只看免预约
        canuse_holiday = (CheckBox) findViewById(R.id.canuse_holiday);//节假日可用
        lunchTime_group = (MutilRadioGroup) findViewById(R.id.lunchTime_group);//用餐时段
        servce_group = (MutilRadioGroup) findViewById(R.id.servce);//餐厅服务
        num_group = (MutilRadioGroup) findViewById(R.id.num);//用餐人数
        reset = (TextView) findViewById(R.id.reset);//用餐人数
        confirm = (TextView) findViewById(R.id.confirm);//用餐人数
        reset.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    /**
     * 获取控件id数据
     */
    private void getData() {
//        用餐时间
        lunchTimeMap.put(R.id.lunchTime_buxian, 0);
        lunchTimeMap.put(R.id.lunchTime_zaocan, 1);
        lunchTimeMap.put(R.id.lunchTime_wucan, 2);
        lunchTimeMap.put(R.id.lunchTime_xiawucha, 3);
        lunchTimeMap.put(R.id.lunchTime_wancan, 4);
        lunchTimeMap.put(R.id.lunchTime_xiaoye, 5);

//       餐厅服务
        lunchServeMap.put(R.id.servce_buxian, 0);
        lunchServeMap.put(R.id.servce_yuding, 1);
//        用餐人数
        lunchCountMap.put(R.id.count_buxian, 0);
        lunchCountMap.put(R.id.count_danren, 1);
        lunchCountMap.put(R.id.count_shuangren, 2);
        lunchCountMap.put(R.id.count_sansiren, 3);
        lunchCountMap.put(R.id.count_wu_shiren, 4);
        lunchCountMap.put(R.id.count_shiren_shang, 5);
        lunchCountMap.put(R.id.count_other, 6);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset:
                resetData();
                break;
            case R.id.confirm:
                getConfirmData();
                break;
        }

    }

    boolean isFreeYuYue;//是否免预约
    boolean isUseHoliday;//是否节假日可用
    int lunchTime;//用餐时间
    int lunchServe;//餐厅服务
    int lunchNum;//用餐人数

    /**
     * 获取选择数据
     */
    private void getConfirmData() {
        if (only_FreeYuYue.isChecked())
            isFreeYuYue = true;
        if (canuse_holiday.isChecked())
            isUseHoliday = true;
        int lunchTime_groupCheckedRadioButtonId = lunchTime_group.getCheckedRadioButtonId();
        int servce_groupCheckedRadioButtonId = servce_group.getCheckedRadioButtonId();
        int num_groupCheckedRadioButtonId = num_group.getCheckedRadioButtonId();
        lunchTime = lunchTimeMap.get(lunchTime_groupCheckedRadioButtonId);//用餐时间
        lunchServe = lunchServeMap.get(servce_groupCheckedRadioButtonId);//餐厅服务
        lunchNum = lunchCountMap.get(num_groupCheckedRadioButtonId);//用餐人数
        expandPopTabView.onDismissForOther();
    }

    /**
     * 重置数据
     */
    private void resetData() {
        only_FreeYuYue.setChecked(false);//免预约
        canuse_holiday.setChecked(true);//节假日可用
        //用餐时间
        int checkedRadioButtonId = lunchTime_group.getCheckedRadioButtonId();
        if (checkedRadioButtonId != R.id.lunchTime_buxian) {
            ((RadioButton) findViewById(checkedRadioButtonId)).setChecked(false);
            ((RadioButton) findViewById(R.id.lunchTime_buxian)).setChecked(true);
        }
        //餐厅服务
        int servceCheckedRadioButtonId = servce_group.getCheckedRadioButtonId();
        if (servceCheckedRadioButtonId != R.id.servce_buxian) {
            ((RadioButton) findViewById(checkedRadioButtonId)).setChecked(false);
            ((RadioButton) findViewById(R.id.servce_buxian)).setChecked(true);
        }
        //用餐人数
        int numCheckedRadioButtonId = num_group.getCheckedRadioButtonId();
        if (numCheckedRadioButtonId != R.id.count_buxian) {
            ((RadioButton) findViewById(checkedRadioButtonId)).setChecked(false);
            ((RadioButton) findViewById(R.id.count_buxian)).setChecked(true);
        }
    }

    public void setCallBackAndData(ExpandPopTabView expandPopTabView, OnSelectListener onSelectListener) {
        if (onSelectListener != null)
            onSelectListener.getValue(isFreeYuYue, isUseHoliday, lunchTime, lunchServe, lunchNum);
    }

    /**
     * 关闭弹窗，显示选中项
     *
     * @param showValue
     */
    public void onSelectItemExandPopView(String showValue) {
        mExpandPopTabView.onExpandPopView();
//        mExpandPopTabView.setToggleButtonText(showValue);
    }
}
